﻿Function Get-FileResponseTimeMetrics($testFilesConfigurationFilePath)
{
    
    $filesToTest=Get-Content -Path $testFilesConfigurationFilePath

    foreach($fileToTest in $filesToTest)
    {
        $currentTime = (get-date -format "yyyy-MM-dd HH:mm:ss.fff")
        $responseTime = Measure-Command {Get-Content $fileToTest}

        $responseTimeMetrics=New-Object -TypeName PSObject -Property @{FilePath=$fileToTest;
                                                 SampleTime=$currentTime;
                                                 ResponseTime=$responseTime.Milliseconds;
                                                 ClientHostName=$env:computername;}

        $responseTimeMetrics

    }
}

Function Store-FileResponseTimeMetrics($environmentShortName)
{
    begin
    {
        Import-Module -DisableNameChecking -Force ..\ConfigurationDatabase\libConfigurationDatabase.psm1
        $sqlConn = new-object ("Data.SqlClient.SqlConnection") (Get-ConfigurationSetting -environmentShortName $environmentShortName -scope "Global"  -name "FileServicesMonitoringDatabaseConnectionString").Value

        $sqlConn.Open()
        $query="INSERT INTO FileResponseTime (FilePath,SampleTime,ResponseTime,ClientHostName) VALUES(@filePath,@sampleTime,@responseTime,@clientHostName)"

        $command = New-Object  Data.SqlClient.SqlCommand $query,$sqlConn

        $varcharDataType=[System.Data.SqlDbType]::VarChar
        $dateTimeDataType=[System.Data.SqlDbType]::DateTime
        $smallIntDataType=[System.Data.SqlDbType]::SmallInt

        $filePath = New-Object System.Data.SqlClient.SqlParameter "@filePath" , $varcharDataType
        $sampleTime=New-Object System.Data.SqlClient.SqlParameter "@sampleTime" , $dateTimeDataType
        $responseTime=New-Object System.Data.SqlClient.SqlParameter "@responseTime" , $smallIntDataType
        $clientHostName=New-Object System.Data.SqlClient.SqlParameter "@clientHostName" , $varcharDataType


        $command.Parameters.Add($filePath) | Out-Null
        $command.Parameters.Add($sampleTime) | Out-Null
        $command.Parameters.Add($responseTime) | Out-Null
        $command.Parameters.Add($clientHostName) | Out-Null
    }
    process
    {
        $_
        $responseTimeMetrics=$_
        $filePath.Value = $responseTimeMetrics.FilePath
        $sampleTime.Value = $responseTimeMetrics.SampleTime
        $responseTime.Value = $responseTimeMetrics.ResponseTime
        $clientHostName.Value = $responseTimeMetrics.ClientHostName
            
        $command.ExecuteNonQuery() | Out-Null    
    }
}